package com.example.research.controller;

import com.example.research.model.ResearchType;
import com.example.research.service.ResearchTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/research-types")
public class ResearchTypeController {
    @Autowired
    private ResearchTypeService researchTypeService;

    @GetMapping
    public String listResearchTypes(Model model) {
        model.addAttribute("researchTypes", researchTypeService.findAll());
        return "research-types/list";
    }

    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("researchType", new ResearchType());
        return "research-types/add";
    }

    @PostMapping("/add")
    public String addResearchType(@ModelAttribute ResearchType researchType) {
        researchTypeService.save(researchType);
        return "redirect:/research-types";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable Integer id, Model model) {
        model.addAttribute("researchType", researchTypeService.findById(id));
        return "research-types/edit";
    }

    @PostMapping("/edit/{id}")
    public String updateResearchType(@PathVariable Integer id, @ModelAttribute ResearchType researchType) {
        researchType.setTypeId(id);
        researchTypeService.save(researchType);
        return "redirect:/research-types";
    }

    @GetMapping("/delete/{id}")
    public String deleteResearchType(@PathVariable Integer id) {
        researchTypeService.delete(id);
        return "redirect:/research-types";
    }
}